package fr.miage.dao.traitements;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import fr.miage.client.metier.entite.Admin;
import fr.miage.client.metier.entite.Categorie;
import fr.miage.client.metier.entite.Civilite;
import fr.miage.client.metier.entite.Client;
import fr.miage.client.metier.entite.Commande;
import fr.miage.client.metier.entite.Produit;
import fr.miage.client.metier.entite.Role;
import fr.miage.client.metier.entite.Ville;

/**
 * @author kristela
 * 
 */
public class ClientDao {

	/**
	 * Retourne une session.
	 * 
	 * @return la session.
	 */
	public static Session getSession() {
		Configuration config = new Configuration();
		config.addClass(Client.class);
		config.addClass(Commande.class);
//		config.addClass(Lignecommande.class);
		config.addClass(Produit.class);
		config.addClass(Admin.class);
		config.addClass(Categorie.class);
		config.addClass(Civilite.class);
		config.addClass(Role.class);
		config.addClass(Ville.class);
		SessionFactory sessionFactory = config.buildSessionFactory();
		Session session = sessionFactory.openSession();
		return session;
	}

	/**
	 * Ajoute un nouveau client dans la base de donnees.
	 * 
	 * @param client
	 *            le admin a ajouter.
	 */
	public static void insertClient(Client client) {
		Session session = getSession();
		if (session == null) {
			System.out.println("Pas de connexion!");
			System.exit(0);
		}
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.saveOrUpdate(client);
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		} finally {
			session.close();
		}

		session.getSessionFactory().close();
	}

	// a voir
	public static Client selectClient(Client client) {
		Session session = getSession();
		if (session == null) {
			System.out.println("Pas de connexion!");
			System.exit(0);
		}
		String sql = getClientCritere(client);
		// Categorie client = session.load(Client.class, client);
		// Integer id= (Integer)session.createSQLQuery("SELECT idClient FROM
		// client WHERE idClient=\""+client.getIdClient()+"\";").uniqueResult();
//		Client cLoad = (Client) session.createSQLQuery(sql).uniqueResult();
		Client cLoad=new Client();
		session.load(cLoad, client.getIdClient());
		session.close();
		session.getSessionFactory().close();
		return cLoad;
	}

	/**
	 * Renvoie la liste de touts les admins.
	 * 
	 * @return tous les admins.
	 */
	public static List<Client> selectAllClient() {
		List<Client> listClient = new ArrayList<Client>();
		Session session = getSession();
		if (session == null) {
			System.out.println("Pas de connexion!");
			System.exit(0);
		}
		listClient = (List<Client>) session.createQuery("Select c from Client c").list();
		ArrayList<Client> lClient=new ArrayList<Client>();
		for (int i = 0; i < listClient.size(); i++) {
			Client client=listClient.get(i);
			Utilitaire.convertClient(client);
			lClient.add(client);
		}
		// listAdmin =session.createSQLQuery("SELECT * FROM Client;").list();
		session.close();
		session.getSessionFactory().close();
		return lClient;
	}

	/**
	 * Effacer le compte epargne correspondant a un numero.
	 * 
	 * @param numero
	 *            le numero du compte.
	 */
	public static void deleteClient(int idClient) {
		Session session = getSession();
		if (session == null) {
			System.out.println("Pas de connexion!");
			System.exit(0);
		} else {
			String sql = "DELETE FROM client WHERE idClient=" + idClient + ";";
			session.createSQLQuery(sql);
			session.close();
			session.getSessionFactory().close();
			// session.delete(client);
		}
	}

	public static void updateClient(Client client) {
		Session session = getSession();
		if (session == null) {
			System.out.println("Pas de connexion!");
			System.exit(0);
		} else {
			session.saveOrUpdate(client);
		}
		session.close();
		session.getSessionFactory().close();
	}

	public static String getClientCritere(Client client) {
		StringBuffer sql = new StringBuffer(
				"SELECT FROM client WHERE idClient=" + client.getIdClient()
						+ " ");
		if (client.getAdresse() != null) {
			sql.append("AND adresse=\"" + client.getAdresse() + "\" ");
		}
		if (client.getEmail() != null) {
			sql.append("AND email=\"" + client.getEmail() + "\" ");
		}
		if (client.getNom() != null) {
			sql.append("AND nom=\"" + client.getNom() + "\" ");
		}
		if (client.getPrenom() != null) {
			sql.append("AND prenom=\"" + client.getPrenom() + "\" ");
		}
		if (client.getTelephone() != null) {
			sql.append("AND telephone=\"" + client.getTelephone() + "\" ");
		}
		if (client.getVille() != null) {
			sql.append("AND ville=" + client.getVille().getIdVille() + " ");
		}
		sql.append(";");
		return sql.toString();
	}

//	public static void main(String[] args) {
//		 Client client=new Client();
//		 client.setIdClient(1);
//		// client.setAdresse("rue ggg");
//		// client.setEmail("alolll");
//		// client.setNom("dodo");
//		// client.setPrenom("gerar");
//		// client.setTelephone("08");
//		// Ville ville = new Ville(1);
//		// client.setVille(ville);
//		Client c=selectClient(client);
//		System.out.println("***********************"+c+"************************");
//		// Client c= ClientDao.selectClient(client);
//		// System.out.println(c);
//		// ClientDao.deleteClient(c);
////		ClientDao.deleteClient(3);
//	}

}
